import React from 'react';
import _ from 'lodash';
import {Select} from 'antd';
import redux from '../_/redux';
@redux('app', {
    data: 'user'
})
@redux('department', {prefix: 'departmentAll', actions: 'load'})
@redux('role', {prefix: 'roleAll', actions: 'load'})
@redux('workstation', {prefix: 'workstationAll', actions: 'load'})
export default class SelectLabel extends React.Component {
    state = {}

    onChangeWs = (val) => {
        this.setState({currentWorkstation: val});
    };
    onChangeDp = (val) => {
        this.setState({currentDepartment: val});
    };

    render() {
        const {data: user, departmentAlldata: departments, roleAlldata: roles, workstationAlldata: workstations} = this.props;
        if (user && departments && roles && workstations) {
            const authData = JSON.parse(user.AuthData);
            //角色所属部门ids
            const roleDpIds = _.reduce(authData.items, (res, k, v) => {
                res[k] = res[k] || [];
                res[k].push(v);
                return res;
            }, {});
            //角色所属工作站ids
            const roleWsIds = {};

            const roleIds = _.uniq(_.values(authData.items));
            const roleObjs = _.reduce(roles, (res, v) => {
                if (_.has(roleDpIds, v.Id)) {
                    res[v.Id] = v;
                    roleWsIds[v.Id] = _.keys(JSON.parse(v.AuthData));
                }
                return res;
            }, {});
            const wsObjs = _.reduce(workstations, (res, v) => {
                res[v.Id] = v;
                return res;
            }, {});

            const dpObjs = _.reduce(departments, (res, v) => {
                res[v.Id] = v;
                return res;
            }, {});

            //用户工作站ids
            const userWsIds = _.uniq(
                _.flatten(
                    _.map(roleIds, i => _.keys(JSON.parse(roleObjs[i].AuthData)))
                )
            );
            //用户工作站
            const userWs = _.map(userWsIds, i => wsObjs[i]);
            let {currentWorkstation, currentDepartment} = this.state;
            const optionsWs = _.map(userWs, i => (<Select.Option key={i.Id} value={i.Id + ''}>{i.Name}</Select.Option>));
            if (currentWorkstation === undefined || !_.includes(userWsIds, currentWorkstation)) {
                currentWorkstation = (_.head(userWs) || {}).Id;
            }
            currentWorkstation = currentWorkstation && (currentWorkstation + '');
            const wsDpIds = _.uniq(
                _.flatMap(roleWsIds, (v, k) => (
                        _.includes(v, currentWorkstation)
                            ? roleDpIds[k]
                            : []
                    )
                )
            );
            const optionsDp = currentWorkstation ?
                _.map(wsDpIds,
                    i => (<Select.Option key={dpObjs[i].Id} value={dpObjs[i].Id + ''}>{dpObjs[i].Name}</Select.Option>)
                )
                :
                [];

            if (currentDepartment === undefined || !_.includes(wsDpIds, currentDepartment)) {
                currentDepartment = _.head(wsDpIds);
            }
            currentDepartment = currentDepartment && (currentDepartment + '');
            return (
                <div className="selectLabel">
                    <div >
                        <span className="label">工作站</span>
                        <Select
                            value={currentWorkstation}
                            className="select"
                            onChange={this.onChangeWs}
                        >
                            {optionsWs}
                        </Select>
                    </div>
                    <div >
                        <span className="label">科室</span>
                        <Select
                            value={currentDepartment}
                            className="select"
                            onChange={this.onChangeDp}
                        >
                            {optionsDp}
                        </Select>
                    </div>
                </div>
            );
        }
        return (<div/>);
    }
}

